package com.chenyue.cm.bean;

import com.chenyue.cm.common.constant.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author LBX
 */
@Data
@ApiModel(description = "统一返回结果")
public class Success<T> {

    @ApiModelProperty(required = true, notes = "返回状态码(0标识成功  其他标识失败，取msg)", example = "0")
    private Integer code;

    @ApiModelProperty(required = true, notes = "返回提示", example = "返回成功")
    private String message;

    @ApiModelProperty(required = true, notes = "返回数据")
    private T result;

    public Success() {
        super();
    }

    public static <T> Success<T> success() {
        return new Success<>(0, "操作成功");
    }

    public static <T> Success<T> success(T result) {
        return new Success<>(0, "操作成功", result);
    }

    public static <T> Success<T> success(String msg, T result) {
        return new Success<>(0, msg, result);
    }

    public static <T> Success<T> success(Integer code, String msg) {
        return new Success<>(code, msg);
    }

    public static <T> Success<T> error() {
        return new Success<>(500, "操作失败");
    }

    public static <T> Success<T> error(String msg) {
        return new Success<>(500, msg);
    }

    public static <T> Success<T> error(Integer code, String msg, T data) {
        return new Success<>(code, msg, data);
    }

    public static <T> Success<T> error(Integer code, String msg) {
        return new Success<>(code, msg);
    }

    public static <T> Success<T> fail(ErrorCode code) {
        return new Success<>(code);
    }

    public Success(ErrorCode code){
        this.code = code.getCode();
        this.message = code.getMsg();
    }

    public Success(Integer code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

    public Success(Integer code, T result) {
        super();
        this.code = code;
        this.result = result;
    }

    public Success(Integer code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

}
